package ipebg.mzk.provider.system.service.impl;

import ipebg.mzk.facade.entity.MsAdminRole;
import ipebg.mzk.facade.entity.MsAdminUserRole;
import ipebg.mzk.facade.system.service.RoleService;
import ipebg.mzk.facade.system.service.UserRoleService;
import ipebg.mzk.provider.system.dao.UserRoleDao;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Transactional
@Service("userRoleServiceImpl")
public class UserRoleServiceImpl implements UserRoleService {

	@Resource(name = "userRoleDao")
	private UserRoleDao userRoleDao;
	
	@Resource(name = "roleServiceImpl")
	private RoleService  roleService;
	
	@SuppressWarnings("unchecked")
	@Override
	public List<MsAdminRole> getRoleListByUserId(String userId) {
		List<MsAdminUserRole> userRoleList = userRoleDao.findByProperty(MsAdminUserRole.class,new String[]{"adminId"},new Object[]{userId});
		List<MsAdminRole> roleList = new ArrayList<MsAdminRole>();
		for(int i = 0;i<userRoleList.size();i++){
			MsAdminRole role = roleService.findMsAdminRoleById(userRoleList.get(i).getRoleId());
			roleList.add(role);
		}
		return roleList;
	}

	@Override
	public void delete(MsAdminUserRole userRole) {
		try {
			userRoleDao.delete(userRole);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	@Override
	public void save(MsAdminUserRole userRole) {
		// TODO Auto-generated method stub
		try {
			userRoleDao.saveOrUpdate(userRole);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<MsAdminUserRole> getUserRoleListByUserId(String userId) {
		// TODO Auto-generated method stub
		List<MsAdminUserRole> userRoleList = userRoleDao.findByProperty(MsAdminUserRole.class,new String[]{"adminId"},new Object[]{userId});
		return userRoleList;
	}

}
